package com.example.myapplication;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView mainText;
    private TextView messageText;
    private TextView cartText;
    private TextView profileText;
    private List<TextView> tabList = new ArrayList<>();

    private MainFragment mainFragment;
    private MessageFragment messageFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);

        initView();//初始化控件

        MainFragment mainFragment = new MainFragment();
        changeFragment(mainFragment);




    }

    @Override
    public void onClick(View v) {
        int clickTabId = v.getId();
        tabChange(clickTabId);
        if (clickTabId == R.id.text_main){

            changeFragment(mainFragment);
        } else if (clickTabId == R.id.text_message) {


            changeFragment(messageFragment);
        } else if (clickTabId == R.id.text_cart) {


            changeFragment(mainFragment);
        }else {

            changeFragment(messageFragment);
        }
    }

    private void changeFragment(Fragment fragment){
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.layout_content,fragment);
        transaction.commit();
    }

    private void initView(){
        mainText = findViewById(R.id.text_main);
        messageText = findViewById(R.id.text_message);
        cartText = findViewById(R.id.text_cart);
        profileText = findViewById(R.id.text_profile);

        mainText.setOnClickListener(this);
        messageText.setOnClickListener(this);
        cartText.setOnClickListener(this);
        profileText.setOnClickListener(this);

        tabList.add(mainText);
        tabList.add(messageText);
        tabList.add(cartText);
        tabList.add(profileText);

        mainFragment = new MainFragment();
        messageFragment = new MessageFragment();

    }

    private void tabChange(int clickedTabId){
        for (TextView tab:tabList) {
            if (tab.getId() == clickedTabId){
                tab.setBackgroundColor(getColor(R.color.tab_checked));
            }else {
                tab.setBackgroundColor(getColor(R.color.tab_unchecked));
            }
        }
    }

    public void change_text(View view) {
        mainFragment.changeColor();
    }


    public void changechecked(String name){
        TextView checkedText = findViewById(R.id.text_checked);
        checkedText.setText("目前选择："+name);
    }
}

